/**
 * 绑定home页面的点击事件
 * @param {type} lng
 * @param {type} App
 * @returns {undefined}
 */
(function (lng, App) {

    var _router = App.Router;
    var _view = App.View;
    var _presenter = App.Presenter;
    var _element = App.Data.Element;
    var _cont = App.Data.Constants;
    var _binder = App.Binder;
    var _template = App.Data.Template;
    var _biz = App.Biz;
    var _Custom = App.Model.Custom;
    var _Person = App.Model.Person;
    var _Deal = App.Model.Deal;

    var $ = lng.dom;
    var $_doms = App.Data.Doms[_element.SECTIONS.CUSTOM];
    var $_section = $_doms.section;
    var $_person_group = $_doms.person_group;
    var $_product_group = $_doms.product_group;
    var $_custom_name = $_doms.custom_name;
    var $_custom_addr = $_doms.custom_addr;

    var _saveCustom = function () {
        var time = _biz.Rules.now();
        var custom;
        var custom_name = $_custom_name.val();
        var custom_addr = $_custom_addr.val();
        if (!custom_name.isEmpty() && !custom_addr.isEmpty()) {
            custom = new _Custom(custom_name, custom_addr, time, time);
        } else {
            // 客户名称或地址不能为空
            return;
        }
        // add persons
        var person_names = $_person_group.find('.first_input');
        var person_tels = $_person_group.find('.second_input');
        var size = person_names.length;
        for (var i = 0; i < size; i++) {
            var person_name = person_names[i].value;
            var person_tel = person_tels[i].value;
            if (!person_name.isEmpty() && !person_tel.isEmpty()) {
                custom.addPerson(new _Person(person_name, person_tel));
            }
        }
        // add products
        var product_names = $_product_group.find('.first_input');
        var product_moneys = $_person_group.find('.second_input');
        var size = product_names.length;
        for (var i = 0; i < size; i++) {
            var product_name = product_names[i].value;
            var product_money = product_moneys[i].value;
            if (!product_name.isEmpty() && !product_money.isEmpty()) {
                custom.addDeal(new _Deal(product_name, product_money, time));
            }
        }
        _biz.CustomBiz.appendCustom(custom);
    };

    _binder.section_binder[_element.SECTIONS.CUSTOM] = {
        'a#custom-btn-save':_saveCustom,
        'a#custom-add-person':function () {
            $_person_group.append(_template.INPUT_GROUP.format('联系人', '联系方式'));
        },
        'a#custom-add-product':function () {
            $_product_group.append(_template.INPUT_GROUP_2.format('产品', '金额'));
        },
        'a#custom-add-visit':function () {

        }
    };
})(Lungo, App);